package com.sketch.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sketch.commom.PageResult;
import com.sketch.commom.Result;
import com.sketch.commom.enums.VipEnum;
import com.sketch.commom.utils.ConversionUtils;
import com.sketch.commom.utils.MapstructUtils;
import com.sketch.entity.DramaSeries;
import com.sketch.entity.SketchInfo;
import com.sketch.entity.Tag;
import com.sketch.entity.dto.DramaSeriesDTO;
import com.sketch.entity.dto.SketchInfoDTO;
import com.sketch.mapper.DramaSeriesMapper;
import com.sketch.mapper.SketchInfoMapper;
import com.sketch.mapper.TagMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SketchInfoService {
    @Resource
    private SketchInfoMapper sketchInfoMapper;
    @Resource
    private DramaSeriesMapper dramaSeriesMapper;
    @Resource
    private TagMapper tagMapper;

    @Transactional
    public Result addOrUpdate(SketchInfoDTO sketchInfoDTO) {
        if (ObjectUtils.isEmpty(sketchInfoDTO.getId())) {
            SketchInfo sketchInfoEntity = new SketchInfo();
            BeanUtils.copyProperties(sketchInfoDTO, sketchInfoEntity);
            sketchInfoMapper.insert(sketchInfoEntity);
            if (!ObjectUtils.isEmpty(sketchInfoDTO.getDramaSeriesDTOS())) {
                sketchInfoDTO.getDramaSeriesDTOS().forEach(e -> {
                    DramaSeries dramaSeries = new DramaSeries();
                    BeanUtils.copyProperties(e, dramaSeries);
                    dramaSeriesMapper.insert(dramaSeries);
                });
            }
        } else {
            SketchInfo sketchInfoEntity = new SketchInfo();
            BeanUtils.copyProperties(sketchInfoDTO, sketchInfoEntity);
            sketchInfoMapper.updateById(sketchInfoEntity);
            dramaSeriesMapper.delete(new LambdaQueryWrapper<DramaSeries>().eq(DramaSeries::getSketchId, sketchInfoDTO.getId()));
            if (!ObjectUtils.isEmpty(sketchInfoDTO.getDramaSeriesDTOS())) {
                sketchInfoDTO.getDramaSeriesDTOS().forEach(e -> {
                    DramaSeries dramaSeries = new DramaSeries();
                    BeanUtils.copyProperties(e, dramaSeries);
                    dramaSeriesMapper.insert(dramaSeries);
                });
            }
        }

        return Result.success();
    }

    public Result list(String name, String status, Integer categoryId, Integer page, Integer size) {
        LambdaQueryWrapper<SketchInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!ObjectUtils.isEmpty(name), SketchInfo::getName, name)
                .eq(!ObjectUtils.isEmpty(status), SketchInfo::getStatus, status)
                .eq(!ObjectUtils.isEmpty(categoryId), SketchInfo::getCategoryId, categoryId);
        Page<SketchInfo> sketchInfoPage = new Page<>(page, size);
        IPage<SketchInfo> result = sketchInfoMapper.selectPage(sketchInfoPage, queryWrapper);
        PageResult dtoPage = new PageResult<>();
        BeanUtils.copyProperties(result, dtoPage);
        List<SketchInfoDTO> list = new ArrayList<>();
        if (ObjectUtils.isEmpty(result.getRecords())) {
            return Result.success(dtoPage);
        }
        result.getRecords().forEach(e -> {
            SketchInfoDTO sketchInfoDTO = new SketchInfoDTO();
            BeanUtils.copyProperties(e, sketchInfoDTO);
            list.add(sketchInfoDTO);
        });
        dtoPage.setList(list);
        return Result.success().withData(dtoPage);
    }

    public Result detaiMore(Long id, Integer page, Integer size) {
        // 一次根据  分类 标签 剧场来筛选
        SketchInfo sketchInfo = sketchInfoMapper.selectById(id);
        String[] categoryIdArr = sketchInfo.getCategoryId().split(",");
        String[] tagIdIdArr = sketchInfo.getTagId().split(",");
        String[] theaterIdArr = sketchInfo.getTheaterId().split(",");
        LambdaQueryWrapper<SketchInfo> queryWrapper = new LambdaQueryWrapper<>();
        for (String c:categoryIdArr
             ) {
            queryWrapper.or(wrapper -> wrapper.like(SketchInfo::getCategoryId,c));
        }
        for (String c:tagIdIdArr
        ) {
            queryWrapper.or(wrapper -> wrapper.like(SketchInfo::getTagId,c));
        }
        for (String c:theaterIdArr
        ) {
            queryWrapper.or(wrapper -> wrapper.like(SketchInfo::getTheaterId,c));
        }
        queryWrapper.orderByDesc(SketchInfo::getCreateTime);
        Page<SketchInfo> sketchInfoPage = new Page<>(page, size);
        IPage<SketchInfo> result = sketchInfoMapper.selectPage(sketchInfoPage, queryWrapper);
        return Result.success().withData(result);
//        PageResult dtoPage = new PageResult<>();
//        BeanUtils.copyProperties(result, dtoPage);
//        return Result.success().withData(dtoPage);
    }

    public Result detail(Long id) {
        SketchInfo sketchInfo = sketchInfoMapper.selectById(id);
        SketchInfoDTO sketchInfoDTO = new SketchInfoDTO();
        BeanUtils.copyProperties(sketchInfo, sketchInfoDTO);
        List<DramaSeries> list = dramaSeriesMapper.selectList(new LambdaQueryWrapper<DramaSeries>().eq(DramaSeries::getSketchId, id)
                .eq(DramaSeries::getIsDel, 0));
            // 处理 分类标签展示
        String[] tagIdArr = sketchInfo.getTagId().split(",");
        List<String> tagNameList = tagMapper.selectList(new LambdaQueryWrapper<Tag>().in(Tag::getId,tagIdArr)).stream().map(e -> {
            return e.getName();
        }).collect(Collectors.toList());
        sketchInfoDTO.setDramaSeriesDTOS(ConversionUtils.convertList(list, DramaSeriesDTO.class));
        sketchInfoDTO.setTagList(tagNameList);
        return Result.success().withData(sketchInfoDTO);
    }

    public Result delete(Long id) {
        sketchInfoMapper.deleteById(id);
        return Result.success();
    }

    public List<String> getTagNameList(String tagId){
        String[] tagIdArr = tagId.split(",");
        return tagMapper.selectList(new LambdaQueryWrapper<Tag>().in(Tag::getId,tagIdArr)).stream().map(e -> {
            return e.getName();
        }).collect(Collectors.toList());
    }

}